package com.common.common.utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.common.R;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

public class AppToast {
    private static Toast mToast;

    public static void showToast(Context context, String content) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_toast_app, null);
        TextView tv_content1 = view.findViewById(R.id.tv_toast_content1);
        tv_content1.setText(content);
        if (mToast != null) {
            mToast.cancel();
        }
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        int widthPixels = displayMetrics.widthPixels;
        //由layout文件创建一个View对象
        LinearLayout.LayoutParams layoutParams = new LinearLayout
                .LayoutParams(getInt(widthPixels * 0.6), ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置TextView的宽度为 屏幕宽度
        tv_content1.setLayoutParams(layoutParams);
        mToast = new Toast(context);
        mToast.setView(view);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();


    }

    public static void showToast(Context context, String content, int timeMillis) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_toast_app, null);
        TextView tv_content1 = view.findViewById(R.id.tv_toast_content1);
        tv_content1.setText(content);
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = new Toast(context);
        mToast.setView(view);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        if (timeMillis > 3000) {
            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mToast.show();
                }
            }, 0, 3000);
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    mToast.cancel();
                    timer.cancel();
                }
            }, timeMillis);
        } else {
            mToast.show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    mToast.cancel();
                }
            }, timeMillis);
        }
    }

    //四舍五入把double转化int整型，0.5进一，小于0.5不进一
    private static int getInt(double number) {
        BigDecimal bd = new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
        return Integer.parseInt(bd.toString());
    }

}
